package ru.budget.mobile.ui.transactions

import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.res.painterResource
import androidx.compose.foundation.layout.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.text.TextStyle
import androidx.compose.foundation.Image
import androidx.compose.material3.Text
import androidx.compose.ui.text.font.*
import ru.budget.mobile.ui.entities.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ModifierInfo
import ru.budget.mobile.ui.theme.*
import androidx.compose.ui.unit.*
import ru.budget.mobile.R

@Composable
@Preview(
    showBackground = true,
    device = "id:Xiaomi POCO M5s"
)
private fun TransactionItemTemplatePreview() =
    TransactionItemTemplate(
        transaction = Transaction(
            id = 1,
            title = "Сосиски",
            category = CategoryEnum.FOOD,
            sum = 235.0,
            type = TransactionType.CONSUMPTION,
            datetime = MyDate()
        ),
        visibleDate = true,
        sum = 235.0
    )

@Composable
fun TransactionItemTemplate(
    transaction: Transaction,
    visibleDate: Boolean,
    sum: Double
) {
    Column(
        content = {
            if (visibleDate) {
                Row {
                    Text(
                        text = transaction.datetime.toString(),
                        style = TextStyle(
                            fontSize = 12.sp,
                            lineHeight = 20.sp,
                            fontFamily = FontFamily(
                                Font(R.font.roboto)
                            ),
                            fontWeight = FontWeight.Normal,
                            letterSpacing = 0.1.sp,
                            color = gray10
                        ),
                        modifier = Modifier
                            .padding(
                                top = if (visibleDate) 16.dp else 0.dp
                            )
                            .fillMaxWidth()
                            .weight(50f)
                    )
                    val char = if (transaction.type == TransactionType.CONSUMPTION) "-" else "+"

                    Text(
                        text = "$char${sum}₽",
                        style = TextStyle(
                            fontSize = 12.sp,
                            lineHeight = 20.sp,
                            fontFamily = FontFamily(
                                Font(R.font.roboto)
                            ),
                            fontWeight = FontWeight.Normal,
                            letterSpacing = 0.1.sp,
                            color = gray10,
                            textAlign = TextAlign.End
                        ),
                        modifier = Modifier
                            .padding(
                                top = if (visibleDate) 16.dp else 0.dp
                            )
                            .fillMaxWidth()
                            .weight(50f)
                    )
                }
            }
            Row(
                content = {
                    Image(
                        painter = painterResource(
                            if (transaction.type == TransactionType.CONSUMPTION) transaction.category.category.icon else R.drawable.salary
                        ),
                        contentDescription = null,
                        modifier = Modifier
                            .padding(
                                end = 16.dp
                            )
                    )

                    TextTemplate(
                        text = if (transaction.type == TransactionType.CONSUMPTION) transaction.title else "Зарплата",
                        fontWeight = FontWeight.Medium,
                        fontSize = 14.sp,
                        visibly = true
                    )

                    TextTemplate(
                        text = "·",
                        fontWeight = FontWeight.Normal,
                        fontSize = 12.sp,
                        modifier = Modifier
                            .padding(
                                start = 8.dp,
                                end = 8.dp
                            ),
                        visibly = transaction.type == TransactionType.CONSUMPTION
                    )

                    TextTemplate(
                        text = transaction.category.category.title,
                        fontWeight = FontWeight.Normal,
                        fontSize = 12.sp,
                        visibly = transaction.type == TransactionType.CONSUMPTION
                    )
                    val char = if (transaction.type == TransactionType.CONSUMPTION) "-" else "+"

                    TextTemplate(
                        text = "$char${transaction.sum} ₽",
                        fontWeight = FontWeight.Normal,
                        fontSize = 14.sp,
                        modifier = Modifier
                            .fillMaxWidth(),
                        textAlign = TextAlign.End,
                        visibly = true
                    )
                },
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier
                    .height(64.dp)
            )
        },
        modifier = Modifier
            .fillMaxWidth()
            .padding(
                start = 16.dp,
                end = 16.dp
            ),
        verticalArrangement = Arrangement.spacedBy(4.dp)
    )
}

@Composable
private fun TextTemplate(
    text: String,
    fontWeight: FontWeight,
    fontSize: TextUnit,
    modifier: Modifier = Modifier,
    textAlign: TextAlign = TextAlign.Center,
    visibly: Boolean
) = if (visibly) {
    Text(
        text = text,
        style = TextStyle(
            fontSize = fontSize,
            lineHeight = 20.sp,
            fontFamily = FontFamily(
                Font(R.font.roboto)
            ),
            fontWeight = fontWeight,
            color = black,
            letterSpacing = 0.1.sp,
            textAlign = textAlign
        ),
        modifier = modifier
    )
} else null
